//
//  CYProjectDetail_XiangQingIntroCollectionViewCell.m
//  cyydt
//
//  Created by chu on 2019/1/24.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYProjectDetail_XiangQingIntroCollectionViewCell.h"
#import "CYProjectDetail_PhonePopView.h"

@interface CYProjectDetail_XiangQingIntroCollectionViewCell ()<PlatformButtonClickDelegate>
{
    UIImage *_shareImage;
}
@property (nonatomic, strong) CYProjectDetail_PhonePopView *ziliaoView;
@end

@implementation CYProjectDetail_XiangQingIntroCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
//    self.renzhengBtn.hidden = YES;
}

- (void)setModel:(CYProjectModel *)model{
    _model = model;
    self.titleLabel.text = model.title;
    self.companyLabel.text = model.company_name;
//    [self setAttributeTextWithLabel:self.companyLabel text:[NSString stringWithFormat:@"%@  ",model.company_name]];
    //标签
    [self.labelView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    UIView *view = [[Tool sharedInstance] setSignView:model.customer andBackgroundColor:RGBOF(0xF5F7FB) andTitleColor:RGBOF(0x97A2B9) Height:20 andFont:[UIFont systemFontOfSize:12] andMargin_x:8 andMargin_y:8 andMaxWidth:kWindowW - 95];
    [self.labelView addSubview:view];
    self.labelHeightConstraint.constant = view.frame.size.height;
    
    self.hangyeLabel.text = model.big_catname;
    if ([model.funds isEqualToString:@"0"]) {
        self.feiyongLabel.text = @"全部";
    }else if ([model.funds isEqualToString:@"10"]){
        self.feiyongLabel.text = @"3万以下";
    }else if ([model.funds isEqualToString:@"15"]){
        self.feiyongLabel.text = @"3-8万";
    }else if ([model.funds isEqualToString:@"5"]){
        self.feiyongLabel.text = @"5-10万";
    }else if ([model.funds isEqualToString:@"6"]){
        self.feiyongLabel.text = @"10-20万";
    }else if ([model.funds isEqualToString:@"11"]){
        self.feiyongLabel.text = @"20万以上";
    }
    self.haopingLabel.text = [NSString stringWithFormat:@"%@%%",model.ratio_sg];
    self.xingquLabel.text = [model.gbooks stringValue];
    
    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:self.model.logo] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
        
    } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
        self->_shareImage = image;
    }];
}

- (void)setAttributeTextWithLabel:(UILabel *)label text:(NSString *)text{
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
    
    [attributedString addAttribute:NSForegroundColorAttributeName value:RGBOF(0x999999) range:NSMakeRange(0, text.length)];
    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:NSMakeRange(0, text.length)];
    
    NSTextAttachment *attchMent = [[NSTextAttachment alloc] init];
    attchMent.bounds = CGRectMake(0, 0, 58, 15);//设置frame
    attchMent.image = [UIImage imageNamed:@"zsxq_yrz"];//设置图片
    NSAttributedString *att = [NSAttributedString attributedStringWithAttachment:attchMent];
    [attributedString appendAttributedString:att];
    label.attributedText = attributedString;
}

- (IBAction)zuxinAction:(UIButton *)sender {
    //获取资料
    NSArray *nibView =  [[NSBundle mainBundle] loadNibNamed:@"CYProjectDetail_PhonePopView" owner:self options:nil];
    CYProjectDetail_PhonePopView *backView = [nibView objectAtIndex:0];
    self.ziliaoView = backView;
    backView.frame = CGRectMake(0, 0, kWindowW, kWindowH);
    backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    __weak typeof(self)weakself = self;
    backView.block = ^(NSString * _Nonnull name, NSString * _Nonnull phone, NSString * _Nonnull remark) {
        NSLog(@"name -- %@",name);
        [weakself LiuYanWithName:name Phone:phone Remark:remark];
    };
    [backView show];
}

- (void)LiuYanWithName:(NSString *)name Phone:(NSString *)phone Remark:(NSString *)remark{
    if (name.length == 0) {
        [[UIApplication sharedApplication].keyWindow makeToast:@"请填写您的姓名" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if (phone.length == 0) {
        [[UIApplication sharedApplication].keyWindow makeToast:@"请填写您的手机号" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if (remark.length == 0) {
        [[UIApplication sharedApplication].keyWindow makeToast:@"留言不能为空" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    [EasyShowLodingView showLoding];
    __weak typeof(self)weakself = self;
    NSString *url = [NSString stringWithFormat:@"%@%@",HOST, @"guest-book/create"];
    NSDictionary *param = @{@"id":self.model.ID, @"type":@"3", @"truename":name, @"mobile":phone, @"content":remark, @"gfrom":@"14"};
    [[XBRequest sharedInstance] postDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        [EasyShowLodingView hidenLoding];
        NSLog(@"留言板 ---- %@",responseResult);
        if ([responseResult objectForKey:@"resError"]) {
            //            [self.view makeToast:ResErrorMessage];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                [weakself.ziliaoView dismiss];
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[@"message"] duration:1.5 position:CSToastPositionCenter];
            }else{
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[@"message"] duration:1.5 position:CSToastPositionCenter];
                
            }
        }
    }];
}

- (IBAction)shareAction:(UIButton *)sender {
    YSActionSheetView * ysSheet=[[YSActionSheetView alloc]initNYSView];
    ysSheet.delegate=self;
    [[UIApplication sharedApplication].keyWindow addSubview:ysSheet];
}

- (void) customActionSheetButtonClick:(YSActionSheetButton *) btn
{
    [self shareImageToPlatformType:btn.tag];
}

- (void)shareImageToPlatformType:(NSInteger)platformType
{
    //2:qq 3:qzone 0:weixin 1:pengyouquan
    //创建分享消息对象
    if (platformType == 2 || platformType == 3) {
        
        NSString *utf8String = self.model.share_url;
        NSString *title = self.model.title;
        NSString *description = self.model.shareContent;
        UIImage *image = nil;
        if (!_shareImage) {
            image = [UIImage imageNamed:@"login_logo"];
        }else{
            image = _shareImage;
        }
        QQApiNewsObject *newsObj = [QQApiNewsObject
                                    objectWithURL:[NSURL URLWithString:utf8String] title:title description:description previewImageData:UIImageJPEGRepresentation(image, 1)];
        SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
        if (platformType == 2) {
            //将内容分享到qq
            [QQApiInterface sendReq:req];
        }else{
            //将内容分享到qzone
            [QQApiInterface SendReqToQZone:req];
        }
        
    }else{
        SendMessageToWXReq *req1 = [[SendMessageToWXReq alloc]init];
        // 是否是文档
        req1.bText =  NO;
        
        //    WXSceneSession  = 0,        /**< 聊天界面    */
        //    WXSceneTimeline = 1,        /**< 朋友圈      */
        //    WXSceneFavorite = 2,
        
        
        req1.scene = (int)platformType;
        
        //创建分享内容对象
        WXMediaMessage *urlMessage = [WXMediaMessage message];
        urlMessage.title = self.model.title;//分享标题
        urlMessage.description = self.model.shareContent;//分享描述
        if (!_shareImage) {
            [urlMessage setThumbImage:[UIImage imageNamed:@"login_logo"]];//分享图片,使用SDK的setThumbImage方法可压缩图片大小
        }else{
            [urlMessage setThumbImage:_shareImage];//分享图片,使用SDK的setThumbImage方法可压缩图片大小
        }
        
        //创建多媒体对象
        WXWebpageObject *webObj = [WXWebpageObject object];
        webObj.webpageUrl = self.model.share_url;//分享链接
        
        //完成发送对象实例
        urlMessage.mediaObject = webObj;
        req1.message = urlMessage;
        
        //发送分享信息
        [WXApi sendReq:req1];
        
    }
}

@end
